import 'dart:math';

import 'package:flutter/material.dart';

//默认无状态
// class DefaultTabController_Widget extends StatelessWidget {
//   const DefaultTabController_Widget({Key? key}) : super(key: key);
//   @override
//   Widget build(BuildContext context) {
//     return DefaultTabController(
//       length: 10,
//       child: Scaffold(
//         appBar: AppBar(
//           toolbarHeight:40,
//           bottom: TabBar(
//             isScrollable: true,
//             onTap:(event){
//               int le=event.bitLength;
//               print("=======${le}===${event.isEven}==${event.isOdd}==${event.isFinite}");
//             },
//             tabs: <Widget>[
//               Text("title1",style: TextStyle(fontSize: 24),),
//               Text("title2"),
//               Text("title3"),
//               Text("title3"),
//               Text("title3"),
//               Text("title3"),
//               Text("title3"),
//               Text("title3"),
//               Text("title3"),
//               Text("title3"),
//             ],
//           ),
//         ),
//         body: TabBarView(
//           children: <Widget>[
//             Text("小明"),
//             Text("小洪"),
//             Text("小里"),
//             Text("小里"),
//             Text("小里"),
//             Text("小里"),
//             Text("小里"),
//             Text("小里"),
//             Text("小里"),
//             Text("小里"),
//           ],
//         ),
//       ),
//     );
//   }
// }

//有状态：
class DefaultTabController_Widget extends StatefulWidget {
  const DefaultTabController_Widget({Key? key}) : super(key: key);

  @override
  _DefaultTabController_WidgetState createState() =>
      _DefaultTabController_WidgetState();
}

class _DefaultTabController_WidgetState
    extends State<DefaultTabController_Widget>
    with SingleTickerProviderStateMixin {
  TabController? _tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(vsync: this, length: 10);
    _tabController!.addListener(() {
        print(_tabController?.index);
        // _tabController?.index=4;
    });
  }
   @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController?.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight:40,
        bottom: TabBar(
          isScrollable: true,
          controller: _tabController,
          tabs: <Widget>[
            Text(
              "title1",
              style: TextStyle(fontSize: 24),
            ),
            Text("title2"),
            Text("title3"),
            Text("title3"),
            Text("title3"),
            Text("title3"),
            Text("title3"),
            Text("title3"),
            Text("title3"),
            Text("title3"),
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          Text("小明"),
          Text("小洪"),
          Text("小里"),
          Text("小里"),
          Text("小里"),
          Text("小里"),
          Text("小里"),
          Text("小里"),
          Text("小里"),
          Text("小里"),
        ],
      ),
    );
  }
}
